package com.ac.util;

import java.util.List;
import java.util.regex.Pattern;

import com.ac.exception.RRException;
import com.ac.response.HttpStatus;

/**
 * 校验工具
 * 
 * @author Veigar
 *
 */
public class ValUtils {

	/**
	 * 不为空
	 */
	public static void notNull(String str, String message) {
		str = StrUtils.replaceBlank(str);
		if (StrUtils.isEmpty(str))
			throw new RRException(message + "不能为空", HttpStatus.BAD_REQUEST);
	}

	/**
	 * 不是List空数组
	 */
	public static void notArray(List<?> list, String message) {
		if (list.size() < 1)
			throw new RRException(message + "不能为空", HttpStatus.BAD_REQUEST);
	}

	/**
	 * 不是字符串空数组
	 */
	public static void notArray(String[] list, String message) {
		if (list.length < 1)
			throw new RRException(message + "不能为空", HttpStatus.BAD_REQUEST);
	}

	/**
	 * 是数字
	 */
	public static void isNumber(String num, String message) {
		if (!Pattern.matches("^[0-9]+$", num)) {
			throw new RRException(message + "应是数字", HttpStatus.BAD_REQUEST);
		}
	}

	/**
	 * 是电话
	 */
	public static void isPhone(String phone) {
		if (!Pattern.matches("^1\\d{10}$", phone)) {
			throw new RRException("手机号格式错误", HttpStatus.BAD_REQUEST);
		}
	}

	/**
	 * 最小长度
	 */
	public static void min(String str, int min, String message) {
		if (str.length() < min)
			throw new RRException(message + "的长度需大于" + min, HttpStatus.BAD_REQUEST);
	}

	/**
	 * 最大长度
	 */
	public static void max(String str, int max, String message) {
		if (str.length() > max)
			throw new RRException(message + "的长度需小于" + max, HttpStatus.BAD_REQUEST);
	}

	/**
	 * 最小和最大长度
	 */
	public static void minAndMax(String str, int min, int max, String message) {
		if (str.length() > max || str.length() < min)
			throw new RRException(message + "的长度需大于" + min + "并且小于" + max, HttpStatus.BAD_REQUEST);
	}

	/**
	 * 最小值
	 */
	public static void min(int str, int min, String message) {
		if (str < min)
			throw new RRException(message + "的值需大于" + min, HttpStatus.BAD_REQUEST);
	}

	/**
	 * 最大值
	 */
	public static void max(int str, int max, String message) {
		if (str > max)
			throw new RRException(message + "的值需小于" + max, HttpStatus.BAD_REQUEST);
	}

	/**
	 * 最小和最大值
	 */
	public static void minAndMax(int str, int min, int max, String message) {
		if (str > max || str < min)
			throw new RRException(message + "的值需大于" + min + "并且小于" + max, HttpStatus.BAD_REQUEST);
	}

}
